package socket.homework2;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

/**
 * @author lhn
 * @date 2024-04-26 21:46
 * 发送端
 */

public class Homework02SenderB {
    public static void main(String[] args) throws IOException {
        DatagramSocket datagramSocket = new DatagramSocket(8889);
        System.out.println("发送端已启动...");
        System.out.println("请输入你的问题");
        Scanner scanner = new Scanner(System.in);
        String s = scanner.next();
        byte[] data = s.getBytes();
        DatagramPacket datagramPacket = new DatagramPacket(
                data, data.length, InetAddress.getByName("192.168.72.1"), 8888
        );
        datagramSocket.send(datagramPacket);

        byte[] buffer = new byte[1024];
        datagramPacket = new DatagramPacket(buffer, buffer.length);
        datagramSocket.receive(datagramPacket);
        int length = datagramPacket.getLength();
        data = datagramPacket.getData();
        String str = new String(data, 0, length);
        System.out.println("A端回复的消息如下\n" + str);
        datagramSocket.close();
    }
}
